package com.shameit.goodqrinfomanager.source.http;


import com.google.gson.Gson;
import com.shameit.goodqrinfomanager.bean.litepal.LoginUser;

import me.goldze.mvvmhabit.utils.SPUtils;

/**
 * 本地数据源，可配合Room框架使用
 * Created by goldze on 2019/3/26.
 */
public class LocalDataSourceImpl implements LocalDataSource {
    private volatile static LocalDataSourceImpl INSTANCE = null;

    public static LocalDataSourceImpl getInstance() {
        if (INSTANCE == null) {
            synchronized (LocalDataSourceImpl.class) {
                if (INSTANCE == null) {
                    INSTANCE = new LocalDataSourceImpl();
                }
            }
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }

    private LocalDataSourceImpl() {
        //数据库Helper构建
    }

    @Override
    public void saveUserName(String userName) {
        SPUtils.getInstance().put("UserName", userName);
    }

    @Override
    public void savePassword(String password) {
        SPUtils.getInstance().put("password", password);
    }

    @Override
    public String getUserName() {

        return SPUtils.getInstance().getString("UserName");
    }

    @Override
    public String getPassword() {

        return SPUtils.getInstance().getString("password");
    }

    @Override
    public LoginUser getLoginUser() {
        return new Gson().fromJson(SPUtils.getInstance().getString("loginuser"), LoginUser.class);
    }

    @Override
    public void saveLoginUser(String userJson) {
        SPUtils.getInstance().put("loginuser", userJson);
    }
}
